﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using mscook.core.domain;
using mscook.core.services;

namespace mscook.core.components
{
    public class ResourceBonusStrategy : AbstractBonusStrategy
    {
        private IList<ResourceBonusSetting> resourceBonusSettings;
        private LoginUser loginUser;
        private ILoginUserService loginUserService;
        private int bonusAlreadyGot;
        private PurchasedCategory categoryOfResource;
        private ResourceBonusSetting resourceBonusSetting;
        private string resourceId;
        private bool ifFindResource = false;
        private bool ifGetAvailableBonus = false;
        public ResourceBonusStrategy(string resourceId, IList<ResourceBonusSetting> resourceBonusSettings, LoginUser loginUser, ILoginUserService loginUserService)
        {
            this.resourceId = resourceId;
            resourceBonusSetting = resourceBonusSettings.Where(x => x.ResourceID == resourceId).FirstOrDefault();
            this.resourceBonusSettings = resourceBonusSettings;
            this.loginUser = loginUser;
            this.loginUserService = loginUserService;
        }
        public override bool CheckEnoughBonus()//检查用户是否有权限获取资源
        {
            bonusAlreadyGot = this.GetAvailableBonus();
            ifGetAvailableBonus = true;
            if (Math.Abs(bonusAlreadyGot ) >= loginUser.Bonus)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public override bool CheckEnoughBonus(int num = 1)//检查用户是否有权限获取资源
        {
            bonusAlreadyGot = this.GetAvailableBonus();
            ifGetAvailableBonus = true;
            if (Math.Abs(bonusAlreadyGot * num) >= loginUser.Bonus)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public bool HashRight()
        {
            if ((resourceBonusSetting == null || resourceBonusSetting.Bonus == 0))
            {
                return true;
            }
            else
            {
                if (loginUser!=null&&( loginUser.IsVip || loginUser.hasPurchased(resourceBonusSetting.ResourceID)))
                {
                    return true;
                }              
            }
            return false;
        }

        #region IBonusStrategy 成员

        protected override bool IfNeedBonus()
        {
            return !HashRight();


        }

        protected override int GetAvailableBonus()
        {
            if (!ifGetAvailableBonus)//lock this
            {

                if (resourceBonusSetting != null)
                {
                    bonusAlreadyGot = resourceBonusSetting.Bonus;
                    categoryOfResource = resourceBonusSetting.PurchasedCategory;
                    ifFindResource = true;
                }
                else
                {
                    bonusAlreadyGot = 0;
                }
            }
            return bonusAlreadyGot;
        }

        protected override bool UpdateUserBonus(int bonus)
        {
            //2013-11-26 JohnHash 调用此方法前判断用户积分是否足够


            this.loginUser.Bonus += bonus;
            if (ifFindResource)
            {
                LoginUser lu = loginUserService.Get(this.loginUser.Id);
                lu.PurchasedThings.Add(new PurchasedThing()
                {
                    RelatedId = resourceId,
                    PurchasedCategory = categoryOfResource,
                    LoginUser = lu
                });
                /* this.loginUser.PurchasedThings.Add(new PurchasedThing()
                 {
                     RelatedId = resourceId,
                     PurchasedCategory=categoryOfResource
                 });*/
                loginUserService.SaveOrUpdate(lu);
            }
            //loginUserService.SaveOrUpdate(this.loginUser);
            return true;
        }

        protected override bool UpdateUserBonus(int bonus, int num)
        {
            if (ifFindResource)
            {
                this.loginUser.Bonus += bonus * num;
                return true;

            }
            return false;

        }

        #endregion
    }
}
